package com.one.rope.mvp.web.common.handle;

import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.exception.BaseException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

  @ExceptionHandler(value = {IllegalArgumentException.class, IllegalStateException.class,
      UnauthorizedException.class, BaseException.class,
      RuntimeException.class})
  protected ResponseEntity<Object> handleConflict(RuntimeException ex, WebRequest request) {
    if (ex instanceof MultipartException) {
      return new ResponseEntity<Object>(ResponseObject.userFailure("文件上传大小超过2M"), HttpStatus.OK);
    }
    if (ex instanceof UnauthorizedException) {
      return new ResponseEntity<Object>(
          ResponseObject.userFailure(AdminBusinessMessage.NOT_VISIT_PERMISSION),
          HttpStatus.FORBIDDEN);
    }
    return new ResponseEntity<Object>(ResponseObject.systemError(ex), HttpStatus.OK);

  }

}